﻿using HLStock.Common;
using HLStock.DBAccess;
using HLStock.Model;
using HLStock.MyExcep;
namespace HLStock.WmsService
{
    public class OrderServiceProvider : BaseServiceProvider<OrderModel>
    {
        public List<OrderModel> ListByKeyword(string keyword)
        {
            if (keyword == null) keyword = "";
            keyword = "%" + keyword + "%";
            var cond1 = CondCollection.True().Lk(nameof(OrderModel.OrderCode), keyword);
            var cond2 = CondCollection.True().Lk(nameof(OrderModel.SubOrderCode), keyword);
            var cond3 = CondCollection.True().Lk(nameof(OrderModel.ProductName), keyword);
            var v1 = List(cond1);
            var v2 = List(cond2);
            var v3 = List(cond3);
            var v = v1.Union(v2).Union(v3).Distinct(new ModelEqualityComparer<OrderModel>());
            return v.ToList();
        }

        public OrderModel SearchBySubOrder(string suborder)
        {
            var cond2 = CondCollection.True().Eq(nameof(OrderModel.SubOrderCode), suborder);
            var result = List(cond2);
            if (result.Count > 1)
            {
                throw new MyDataOptException("目标订单只能有一笔，但该条件查出两笔");
            }
            if (result.Count == 0)
            {
                throw new MyDataOptException("找不到对应的记录");
            }
            return result.FirstOrDefault();
        }
    }
}
